Skip to content

refactor(api): requireAuth returns resolved Principal (closes #194)#864

Open
cristim wants to merge 3 commits into
feat/multicloud-web-frontendfrom
fix/194-wave18
Open

refactor(api): requireAuth returns resolved Principal (closes #194)#864
cristim wants to merge 3 commits into
feat/multicloud-web-frontendfrom
fix/194-wave18

Conversation

@cristim
Copy link
Copy Markdown
Member

@cristim cristim commented May 30, 2026

Summary

  • requireAuth now returns (*Principal, error) instead of bare error by delegating to the new authenticatePrincipal helper (already added in this branch)
  • authenticatePrincipal cleaned up: nil-auth guard moved before the API-key path (fail-closed per feedback_fail_closed_middleware.md), dead userIface/userFields locals removed
  • Router updated to _, err := r.h.requireAuth(...) to handle the new return shape
  • Existing TestRequireAuth_* tests upgraded to assert the returned Principal (kind, role, UserID, Session pointer) for each credential type

Test plan

  • go build ./... clean
  • go test github.com/LeanerCloud/CUDly/internal/api/... github.com/LeanerCloud/CUDly/internal/auth/... — 1852 passed
  • TestRequireAuth_AdminAPIKey now asserts PrincipalAdminAPIKey / role=admin
  • TestRequireAuth_UserSession now asserts PrincipalSession, UserID, Role, Session pointer
  • TestRequireAuth_NoCredential_Rejects still asserts 401 ClientError

Closes #194

authenticatePrincipal resolves the caller's identity once across all
three credential kinds (admin API key, user API key, bearer session)
and returns a *Principal. requireAuth now delegates to it instead of
calling authenticate() which threw the resolved identity away.

Router updated to _, err := r.h.requireAuth(...). Nil-auth guard moved
before the API-key path so a missing auth service fails closed (401)
rather than falling through. Dead userIface/userFields locals removed.

TestRequireAuth_* tests extended to assert the returned Principal (Kind,
Role, UserID, Session pointer) for each credential type.
@cristim cristim added triaged Item has been triaged priority/p3 Polish / idea / may never ship severity/low Minor harm urgency/eventually No deadline impact/internal Team-internal only effort/s Hours type/chore Maintenance / non-user-visible labels May 30, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

Warning

Review limit reached

@cristim, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 72 minutes and 9 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d934cc98-465f-41a3-bf48-25b6c6958ae1

📥 Commits

Reviewing files that changed from the base of the PR and between 4956d66 and f6d3dc5.

📒 Files selected for processing (3)
  • internal/api/middleware.go
  • internal/api/router.go
  • internal/api/router_authuser_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/194-wave18

Comment @coderabbitai help to get the list of available commands and usage tips.

@cristim
Copy link
Copy Markdown
Member Author

cristim commented May 30, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Extract principalFromUserAPIKey and principalFromBearerToken helpers
from authenticatePrincipal to bring its cyclomatic complexity from 11
down to 4, passing the gocyclo <=10 gate.
@cristim
Copy link
Copy Markdown
Member Author

cristim commented Jun 2, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 2, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cristim
Copy link
Copy Markdown
Member Author

cristim commented Jun 4, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 4, 2026

Rate Limit Exceeded

@cristim have exceeded the limit for the number of chat messages per hour. Please wait 35 minutes and 20 seconds before sending another message.

…I-key and nil-auth paths (refs #194)

When ValidateUserAPIKeyAPI returns a userRaw value whose concrete type does
not satisfy the GetID/GetEmail/GetRole interface, principalFromUserAPIKey
previously returned a partially-populated Principal (Role:"user", empty
UserID/Email). Any handler trusting the Principal would have granted access
to an unresolved identity. Fix: return nil (deny) when the assertion fails.

Adds three tests:
- TestRequireAuth_UserAPIKey: valid user API key yields PrincipalUserAPIKey
  with populated UserID/Email/Role (zero coverage on the #178 hot-spot).
- TestRequireAuth_UserAPIKey_BadRecord: unexpected userRaw type yields 401;
  fails pre-fix (Principal returned), passes post-fix (nil returned).
- TestRequireAuth_NilAuth_Rejects: Handler with auth==nil returns 401 for
  a non-admin key, confirming the fail-closed nil-auth branch.
@cristim
Copy link
Copy Markdown
Member Author

cristim commented Jun 5, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 5, 2026

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/s Hours impact/internal Team-internal only priority/p3 Polish / idea / may never ship severity/low Minor harm triaged Item has been triaged type/chore Maintenance / non-user-visible urgency/eventually No deadline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant